<template>
  <div class="add-form">
    <el-dialog
      :title="titleInfo.text + titleInfo.pageTitle"
      :visible="dialogFormVisible"
      @close="dialogFormH"
    >
      <el-form ref="form" :model="form" label-width="100px" :rules="rules">
        <el-form-item label="策略名称：" prop="policyName" style="width: 500px;">
          <el-input
            v-model="form.policyName"
            type="text"
            placeholder="请输入"
            maxlength="15"
            show-word-limit
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="策略方案：" prop="discount">
          <el-input-number v-model="form.discount" :min="0" :max="100" placeholder="请输入" controls-position="right" autocomplete="off" style="width: 396px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="dialogFormH">取消</el-button>
          <el-button @click="onSubmit">确认</el-button>
        </el-form-item>

      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { addTactics, updateTactics } from '@/api/aliceRequests'
export default {
  name: '',
  components: {

  },
  props: {
    titleInfo: {
      type: Object,
      required: true
    },
    dialogFormVisible: {
      type: Boolean,
      default: false
    },
    policyId: {
      type: Number,
      required: true
    },
    pageParams: {
      type: Object,
      required: true
    },
    policyName: {
      type: String,
      required: true
    },
    discount: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      text: '',
      num: null,
      form: {
        policyName: '',
        discount: 0,
        policyId: 0
      },
      rules: {
        policyName: [{ required: true }],
        discount: [{ required: true }]
      }

    }
  },
  created() {
    // this.getvmListDetail()
  },
  methods: {
    // 弹层隐藏
    dialogFormH() {
      this.form = {
        policyName: '',
        discount: 0,
        policyId: 0
      }
      this.$refs.form.resetFields()
      this.$emit('update:dialogFormVisible', false)
    },

    // 点击提交发起新增和编辑功能
    async onSubmit() {
      try {
        // 表单验证
        await this.$refs.form.validate()
        if (this.form.policyId) {
          // 编辑场景
          await updateTactics(this.form)
          this.$message.success('修改部门成功')
        } else {
          this.form.policyId = this.policyId
          await addTactics(this.form)
          this.$message.success('添加部门成功')
        }
        this.dialogFormH()
        this.$parent.getTacticsList()
      } catch (error) {
        console.log(error)
      }
    },
    // 获取表单详情数据
    getPolicyDetail() {
      this.form.policyId = this.policyId
      this.form.policyName = this.policyName
      this.form.discount = this.discount
    }

  }
}
</script>

<style scoped lang='scss'>

</style>
